package ly.gupao.edu.thread.ExecuteByOrder;

/**
 * @author: liyang
 * @date：2019/3/11 0011
 * describe：
 */
public class WiteNotifyAll {

    static int count = 0;

    public static void main(String[] args) {

        final Object obj = new Object();

        Thread t1 = new Thread(() -> {
            while(true){
                synchronized (obj){
                    if(count % 3 == 0){
                        System.out.println("Thread1 ---:" + count);
                        count ++;
                        obj.notifyAll();
                    }else{
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Thread t2 = new Thread(() -> {
            while(true){
                synchronized (obj){
                    if(count % 3 == 1){
                        System.out.println("Thread2 ---:" + count);
                        count ++;
                        obj.notifyAll();
                    }else{
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Thread t3 = new Thread(() -> {
            while(true){
                synchronized (obj){
                    if(count % 3 == 2){
                        System.out.println("Thread3 ---:" + count);
                        count ++;
                        obj.notifyAll();
                    }else{
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }

}
